package com.agriculture.competition.controller;

import com.agriculture.common.core.utils.poi.ExcelUtil;
import com.agriculture.common.core.web.controller.BaseController;
import com.agriculture.common.core.web.domain.AjaxResult;
import com.agriculture.common.core.web.page.TableDataInfo;
import com.agriculture.common.log.annotation.Log;
import com.agriculture.common.log.enums.BusinessType;
import com.agriculture.competition.domain.JaDateTable;
import com.agriculture.competition.domain.JaFile;
import com.agriculture.competition.domain.vo.ExcelLineVo;
import com.agriculture.competition.domain.vo.JaDateTableDownLoadVo;
import com.agriculture.competition.service.IJaDateTableService;
import com.agriculture.competition.utils.ExcelDownLoadUtil;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 数据管理Controller
 *
 * @author agriculture
 * @date 2024-11-12
 */
@RestController
@RequestMapping("/table")
public class JaDateTableController extends BaseController {

    @Autowired
    private IJaDateTableService jaDateTableService;

    /**
     * 查询数据管理列表
     */
    // @RequiresPermissions("competition:table:list")
    @GetMapping("/list")
    public TableDataInfo list(JaDateTable jaDateTable) {
        startPage();
        List<JaDateTable> list = jaDateTableService.selectJaDateTableList(jaDateTable);
        return getDataTable(list);
    }

    /**
     * 导出数据管理列表
     */
    // @RequiresPermissions("competition:table:export")
    @Log(title = "数据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JaDateTable jaDateTable) {
        List<JaDateTable> list = jaDateTableService.selectJaDateTableList(jaDateTable);
        ExcelUtil<JaDateTable> util = new ExcelUtil<JaDateTable>(JaDateTable.class);
        util.exportExcel(response, list, "数据管理数据");
    }

    /**
     * 获取数据管理详细信息
     */
    //@RequiresPermissions("competition:table:query")
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable("tableId") Long tableId) {
        return success(jaDateTableService.selectJaDateTableByTableId(tableId));
    }

    /**
     * 新增数据管理
     */
    // @RequiresPermissions("competition:table:add")
    @Log(title = "数据管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JaDateTable jaDateTable) {
        return toAjax(jaDateTableService.insertJaDateTable(jaDateTable));
    }

    /**
     * 修改数据管理
     */
    // @RequiresPermissions("competition:table:edit")
    @Log(title = "数据管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JaDateTable jaDateTable) {
        return toAjax(jaDateTableService.updateJaDateTable(jaDateTable));
    }

    /**
     * 删除数据管理
     */
    // @RequiresPermissions("competition:table:remove")
    @Log(title = "数据管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tableIds}")
    public AjaxResult remove(@PathVariable Long[] tableIds) {
        return toAjax(jaDateTableService.deleteJaDateTableByTableIds(tableIds));
    }

    /**
     * 根据分类id查询分类下的excel名
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryExcelName")
    public AjaxResult queryExcelName(@RequestParam("id") Long id) {
        return success(jaDateTableService.queryExcelName(id));
    }

    @PostMapping("/download")
    public void download(HttpServletResponse response, @RequestBody JaDateTableDownLoadVo jaDateTableDownLoadVo)
            throws IOException {
        ExcelDownLoadUtil.downLoad(response, jaDateTableDownLoadVo);
    }


    /**
     * 查excel的数据内容，根据文件url
     *
     * @param url
     * @return
     */
    @GetMapping(value = "/queryExcelData")
    public AjaxResult queryExcelData(@RequestParam("url") String url, HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        // "/temp" 是服务器临时目录的虚拟路径，根据实际情况可能会有所不同
        String tempDir = servletContext.getRealPath("/temp");
        logger.info("queryExcelData===开始===" + tempDir);
        return success(jaDateTableService.queryExcelData(url, tempDir));
    }


    /**
     * 根据选中的标题名和文件路径来查某一列的数据
     *
     * @param excelLineVo
     * @return
     */
    @PostMapping(value = "/queryExcelLine")
    public AjaxResult queryExcelLine(@RequestBody ExcelLineVo excelLineVo, HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        // "/temp" 是服务器临时目录的虚拟路径，根据实际情况可能会有所不同
        String tempDir = servletContext.getRealPath("/temp");
        logger.info("queryExcelLine===开始===" + tempDir);
        excelLineVo.setTempDir(tempDir);
        return success(jaDateTableService.queryExcelLine(excelLineVo));
    }

    /**
     * 更具多选的excel标题去返回不同的excel的标题列数据
     * 参数  lineVoList
     *
     * @param excelLineVo
     * @return
     */
    @PostMapping(value = "/queryExcelMoreLine")
    public AjaxResult queryExcelMoreLine(@RequestBody ExcelLineVo excelLineVo, HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        // "/temp" 是服务器临时目录的虚拟路径，根据实际情况可能会有所不同
        String tempDir = servletContext.getRealPath("/temp");
        excelLineVo.setTempDir(tempDir);
        return success(jaDateTableService.queryExcelMoreLine(excelLineVo));
    }

    // @RequiresPermissions("competition:table:upload")
    @PostMapping("/updateFile")
    public AjaxResult updateFile(@RequestBody JaFile jaFile) {
        return success(jaDateTableService.updateFile(jaFile));
    }

}
